package com.shqcjd.sinopecdemomobile.ui.fragment.problem;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.app.MyApp;
import com.shqcjd.sinopecdemomobile.greendao.OfflineProblemBeanDao;
import com.shqcjd.sinopecdemomobile.model.pojo.OfflineProblemBean;
import com.shqcjd.sinopecdemomobile.ui.activity.problem.ProblemFeedbackActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.OfflineProblemAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseFragment;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.PrefUtils;

import java.util.List;

import butterknife.BindView;

/**
 * Created by sy on 2019/5/14.
 */

public class OfflineProblemFragment extends BaseFragment{
    @BindView(R.id.rv_offline_list)
    RecyclerView rvProblemList;

    private Context mContext;
    private View notDataView;
    private OfflineProblemAdapter offlineProblemAdapter;
    private List<OfflineProblemBean> offlineProblemBeans;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    public static OfflineProblemFragment newInstance() {
        return new OfflineProblemFragment();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_offline_problem_layout;
    }

    @Override
    public void initView(View rootView) {
        mContext = getContext();
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvProblemList.getParent(), false);
        initProblem();
    }

    @Override
    public void onResume() {
        super.onResume();
        int loginId = PrefUtils.getInt(mContext, PrefUtils.USER_ID, -1);
        offlineProblemBeans=MyApp.getmDaoSession().getOfflineProblemBeanDao().queryBuilder().where(OfflineProblemBeanDao.Properties.LoginUserId.eq(loginId)).orderDesc(OfflineProblemBeanDao.Properties.Time).list();
        if (offlineProblemBeans!=null&&offlineProblemBeans.size() > 0) {
            offlineProblemAdapter.setNewData(offlineProblemBeans);
        } else {
            offlineProblemAdapter.setNewData(null);
            offlineProblemAdapter.setEmptyView(notDataView);
        }
    }

    private void initProblem() {

        offlineProblemAdapter = new OfflineProblemAdapter();
        rvProblemList.setLayoutManager(new LinearLayoutManager(getContext()));
        rvProblemList.setAdapter(offlineProblemAdapter);
//        allProblemAdapter.setOnLoadMoreListener(this, rvProblemList);
        offlineProblemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OfflineProblemBean item = (OfflineProblemBean) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.content:
                        Intent intent = new Intent(getContext(), ProblemFeedbackActivity.class);
                        intent.putExtra("offlineProblemId", item.getId());
                        startActivity(intent);
                        break;
                    case R.id.right:
                        MyApp.getmDaoSession().getOfflineProblemBeanDao().deleteByKey(item.getId());
                        adapter.remove(position);
                        break;
                }


            }
        });


    }



}
